
//
//  YKStatuesTool.m
//  youkan
//
//  Created by mac mini on 16/2/29.
//  Copyright © 2016年 Honsintech. All rights reserved.
//  处理微博数据

#import "YKStatuesTool.h"
#import "YKWBStatues.h"
#import "YKAccountTool.h"
#import "YKAccount.h"
#import "YKHttpTool.h"
#import "YKStatusParam.h"
#import "YKStatusResult.h"
#import "YKStatusCacheTool.h"

@implementation YKStatuesTool
+(void)newStatusWithSinceId:(NSString*)sinceId success:(void(^)(NSArray*statuses)) succes failure:(void(^)(NSError *error))failure {
//    创建参数模型
    YKStatusParam *param=[[YKStatusParam alloc]init];
    param.access_token =[YKAccountTool account].access_token;
//    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    if (sinceId) {// 有微博数据才刷新
        param.since_id =sinceId;
    }
//    params[@"access_token"]= [YKAccountTool account].access_token;
    NSArray *statuses=[YKStatusCacheTool statusesWithParam:param];
    if (statuses.count) {
        if (succes) {
            succes(statuses);
        }
        return;
    }
    
    [YKHttpTool GET:kWBTimelineURL parameters:param.keyValues success:^(id responseObject) {
        YKStatusResult *result =[YKStatusResult objectWithKeyValues:responseObject];

//        NSArray *dictArr =responseObject[@"statuses"];
//        NSArray *statuses = (NSMutableArray*)[YKStatues objectArrayWithKeyValuesArray:dictArr];
        if (succes) {
            succes(result.statuses);
        }
        [YKStatusCacheTool saveWithStatus:responseObject[@"statuses"]];

    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
}
+(void)moreStatusWithMaxId:(NSString *)maxId success:(void (^)(NSArray *))succes failure:(void (^)(NSError *))failure{
    YKStatusParam *param =[[YKStatusParam alloc]init];
    if (maxId) {
        long long maxid = [maxId  longLongValue];
        param.max_id=[NSString stringWithFormat:@"%lld",maxid-1];
    }
    param.access_token=[YKAccountTool account].access_token;
//        NSMutableDictionary *param=[NSMutableDictionary dictionary];
//    if (maxId) {
//        long long maxid = [maxId  longLongValue];
//        param[@"max_id"]=[NSString stringWithFormat:@"%lld",maxid-1];
//    }
//    param[@"access_token"]=[YKAccountTool account].access_token;
//      先从缓存中取数据
    NSArray *statuses=[YKStatusCacheTool statusesWithParam:param];
    if (statuses.count) {
        if (succes) {
            succes(statuses);
        }
        return;
    }
    [YKHttpTool GET:kWBTimelineURL parameters:param.keyValues success:^(id responseObject) {
        YKStatusResult *result=[YKStatusResult objectWithKeyValues:responseObject];
//        NSArray *dictArr =responseObject[@"statuses"];
//        NSArray *statuses = (NSMutableArray*)[YKStatues objectArrayWithKeyValuesArray:dictArr];
        if (succes) {
            succes(result.statuses);
        }
//        将获取到的数据缓存起来
        [YKStatusCacheTool saveWithStatus:responseObject[@"statuses"]];
    } failure:^(NSError *error) {

    }];
}
@end
